package com.lsfb.signln.Login.Forget;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.alimuzaffar.lib.pin.PinEntryEditText;
import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.net.MD5;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.HashMap;

/**
 * 验证码
 * Created by Administrator on 2018/1/23 0023.
 */
@ContentView(R.layout.aty_forget_code)
public class ForgetCodeActicity extends LsfbActivity {

    /**
     * 手机号码
     */
    @ViewInject(R.id.aty_code_text_phone)
    private TextView text_phone;

    /**
     * 验证码
     */
    @ViewInject(R.id.txt_pin_entry)
    private PinEntryEditText txt_pin_entry;

    /**
     * 倒计时
     */
    @ViewInject(R.id.aty_code_time)
    private TextView code_time;

    private TimeCount time;

    private String phone;
    private String code;

    @Override
    public void initView() {
        phone = getIntent().getStringExtra("phone");
        code = getIntent().getStringExtra("code");
        text_phone.setText(phone);
        time = new TimeCount(60000, 1000);
        time.start();// 开始计时
    }

    @Override
    public void initEvent() {
        if (txt_pin_entry != null) {
            txt_pin_entry.setOnPinEnteredListener(new PinEntryEditText.OnPinEnteredListener() {
                @Override
                public void onPinEntered(CharSequence str) {

                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(txt_pin_entry.getWindowToken(), 0);
                    if (MD5.GetMD5Code(MD5.GetMD5Code(str.toString())).equals(code)) {
                        startActivity(new Intent(ForgetCodeActicity.this, ForgetPwdActicity.class).putExtra("code", str.toString()).putExtra("phone", phone));
                    } else {
                        T.showShort(getApplicationContext(), "验证码错误");
                        txt_pin_entry.setText(null);
                    }
                }

            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        txt_pin_entry.setText(null);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_forget_img_back, R.id.aty_code_time})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.aty_forget_img_back:
                finish();
                break;
            //获取验证码
            case R.id.aty_code_time:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(txt_pin_entry.getWindowToken(), 0);
                T.showShort(this, "验证码发送中...");
                getForgetPwdcc();
                break;
        }
    }

    /**
     * 获取验证码
     */
    public void getForgetPwdcc() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("phone", phone);
        new BaseInternet().getData_Get(URLString.GETFORGETPWDCC, hashMap, String.class, new GetForgetPwdccEvent2(), false, false);
    }

    @EventAnnotation
    public void GetForgetPwdccEvent2(GetForgetPwdccEvent2 event) {
        switch (event.getCode()) {
            case 200:
                time.start();// 开始计时
                T.showShort(this, "验证码发送成功，请注意收取手机短信。");
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    @Override
    public void initData() {

    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            code_time.setText("重新获取验证码");
            code_time.setTextColor(getResources().getColor(R.color.theme_color));
            code_time.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            code_time.setClickable(false);//防止重复点击
            code_time.setTextColor(getResources().getColor(R.color.hint_font));
            code_time.setText("重新获取验证码(" + millisUntilFinished / 1000 + ")");
        }

    }
}
